﻿// Written by Josh Smith - 9/2007
using System.Reflection;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System;
using System.Windows;

namespace Client.components
{
    /// <summary>
    /// A Slider which provides a way to modify the 
    /// auto tooltip text by using a format string.
    /// </summary>
    public class SliderF : Slider
    {
        ToolTip _autoToolTip;

        public String AutoToolTipContent
        {
            get { return (String)GetValue(AutoToolTipContentProperty); }
            set { SetValue(AutoToolTipContentProperty, value); }
        }

        public static readonly DependencyProperty AutoToolTipContentProperty = DependencyProperty.Register(
            "AutoToolTipContent",
            typeof(String),
            typeof(SliderF),
            new FrameworkPropertyMetadata(null));

        protected override void OnThumbDragStarted(DragStartedEventArgs e)
        {
            base.OnThumbDragStarted(e);
            this.ApplyAutoToolTipContent();    
        }

        protected override void OnThumbDragDelta(DragDeltaEventArgs e)
        {
            base.OnThumbDragDelta(e);
            this.ApplyAutoToolTipContent();
        }

        private void ApplyAutoToolTipContent()
        {
            if (AutoToolTipContent != null && AutoToolTip != null)
            {
                this.AutoToolTip.Content = AutoToolTipContent;
            }
        }

        private ToolTip AutoToolTip
        {
            get
            {
                if (_autoToolTip == null)
                {
                    FieldInfo field = typeof(Slider).GetField(
                        "_autoToolTip",
                        BindingFlags.NonPublic | BindingFlags.Instance);

                    _autoToolTip = field.GetValue(this) as ToolTip;
                }

                return _autoToolTip;
            }
        }
    }
}